package cn.sumpu.app.guoguojie.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.ExperBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.dao.ExperDao;
import cn.sumpu.app.guoguojie.domain.MemberDetail;
import cn.sumpu.app.guoguojie.domain.MemberLearning;
import cn.sumpu.app.guoguojie.domain.MemberWork;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class ExperBizImpl implements ExperBiz {

	@Resource
	private IdentifierBiz identifierBiz;
	
	private ExperDao experDao;

	public void setExperDao(ExperDao experDao) {
		this.experDao = experDao;
	}

	@Override
	public List<MemberLearning> getLearningList(String member)
			throws AppBusinessException {
		return experDao.getLearningList(member);
	}

	@Override
	public MemberLearning getLearningById(String id)
			throws AppBusinessException {

		return experDao.getLearningById(id);
	}

	@Override
	public List<MemberWork> getWorkList(String member)
			throws AppBusinessException {
		return experDao.getWorkList(member);
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void saveExper(MemberDetail detail) throws AppBusinessException {
		if (detail == null)
			return;
		List<MemberLearning> learningList = detail.getLearningList();
		List<MemberWork> workList = detail.getWorkList();
		experDao.deleteLearning(detail.getMember());
		experDao.deleteWork(detail.getMember());
		for (MemberLearning learning : learningList) {
			learning.setMember(detail.getMember());
			learning.setIdentifier(identifierBiz.generateId("member_learning"));
			experDao.insertLearning(learning);
		}
		for (MemberWork work : workList) {
			work.setMember(detail.getMember());
			work.setIdentifier(identifierBiz.generateId("member_work"));
			experDao.insertWork(work);
		}

	}

	@Override
	public void updateLearning(MemberLearning learning)
			throws AppBusinessException {
		experDao.updateLearning(learning);
	}

	@Override
	public void deleteLearningById(String id) throws AppBusinessException {
		experDao.deleteLearningById(id);
	}

	@Override
	public void updateWork(MemberWork work) throws AppBusinessException {
		experDao.updateWork(work);
	}

	@Override
	public void deleteWorkById(String id) throws AppBusinessException {
		experDao.deleteWorkById(id);
	}

	@Override
	public void saveLearning(MemberLearning learning)
			throws AppBusinessException {
		if (learning == null)
			return;
		if (Common.empty(learning.getIdentifier())) {
			if (!"".equals(learning.getSchool())
					&& learning.getSchool() != null)
				learning.setIdentifier(identifierBiz.generateId("member_learning"));
				experDao.insertLearning(learning);
		} else {
			experDao.updateLearning(learning);
		}
	}

	@Override
	public void saveWork(MemberWork work) throws AppBusinessException {
		if (work == null)
			return;
		if (Common.empty(work.getIdentifier())) {
			work.setIdentifier(identifierBiz.generateId("member_work"));
			experDao.insertWork(work);
		} else {
			experDao.updateWork(work);
		}
	}

	@Override
	public MemberWork getWorkById(String id) throws AppBusinessException {
		return experDao.getWorkById(id);
	}

}
